<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/26
 * Time: 14:11
 */
namespace app\index\controller;

use think\Controller;

use think\Db;

use app\common\lib\JsApi;

use app\index\controller\WxUserInfo;

Class WxUser extends Controller
{


    //获取微信信息接口地址
    public function wxInfo()
    {
        $code = input("code");
        $parent_id = input("parent_id");
        $lock = input("lock");
        //file_put_contents("first_block.txt", "邀请parent_id3" . $parent_id . PHP_EOL, FILE_APPEND);
        $appId = config("wx.appid");
        $appsecret = config("wx.appsecret");
        $wxUserInfo = WxUserInfo::getInstance($appId, $appsecret, $code);
        $userInfo = $wxUserInfo->init();
        //file_put_contents("openid.txt", "openid" . json_encode($userInfo) . PHP_EOL, FILE_APPEND);
        $wx_userinfo = Db::name("wx_users")->where("openid", $userInfo["openid"])->find();
        header("Content-type:text/html;charset=utf-8");
        $wx_user["openid"] = $userInfo["openid"];
        $wx_user["nickname"] = $userInfo["nickname"];
        $wx_user["gender"] = $userInfo["sex"] == 1 ? "男" : "女";
        $wx_user["city"] = $userInfo["city"];//龙岩
        $wx_user["province"] = $userInfo["province"];//福建
        $wx_user["country"] = $userInfo["country"];//中国
        $wx_user["headimgurl"] = $userInfo["headimgurl"];//头像链接
        $wx_user["update_time"] = time();
        if (!$wx_userinfo) {
            $wx_user["create_time"] = time();
            if($wx_user["openid"]) {
                createLog("register",json_encode($wx_user));
                Db::name("wx_users")->insert($wx_user);
            }
        } else {
            if ($wx_userinfo["uid"]) {
                $wx_user["uid"] = $wx_userinfo["uid"];
            }
            Db::name("wx_users")->where("openid", $userInfo["openid"])->update($wx_user);
        }

        session("userinfo", $wx_user);

        if ($wx_user["uid"]) {//有用户id的时候

            $user_info = Db::name("user")->where("uid",$wx_user["uid"])->find();

            if($user_info["black"]==1){
                return $this->fetch("shop/forbidden");
                //renderJson("该账户列入黑名单",0);
            }
            $real_status = Db::name("user_real")->where("uid", $wx_user["uid"])->value("real_status");
            //file_put_contents("real_status.txt", "zhengfuzhe:" . $real_status, FILE_APPEND);
            if ($real_status) {
                if ($real_status != 2) {
                    return $this->fetch("shop/first", ["real_status" => $real_status]);
                } else {
                    $this->redirect("index/index");
                }
            } else {
                return $this->fetch("shop/first", ["real_status" => ""]);
            }

        } else {//没有用户id的时候

            $value = Db::name("system")->where("key","sys_start_up_register")->value("value");
            if($value==1) {

                return $this->fetch("shop/register", ["parent_id" => $parent_id]);

            }else{
                
                if ($lock == config("app.sys_lock")) {

                    return $this->fetch("shop/register", ["parent_id" => $parent_id]);

                } else {

                    $this->redirect("web/index");

                }

            }


        }


    }


    public function register_html()
    {
        return $this->fetch("shop/register");
    }


    //https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx46d2655cc7128877&redirect_uri=http://ep.jingtanhaoshop.com/index/Wx_User/wxInfo&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect
    public function wxTowxInfo($parent_id = NULL,$lock=NULL)
    {
        $appid = config('wx.appid');
        file_put_contents("first_block.txt", "邀请parent_id4" . $parent_id . PHP_EOL, FILE_APPEND);
        if ($parent_id) {
            $url = "http://www.zhengfuz.com/index/Wx_User/wxInfo?parent_id=" . $parent_id."&lock=".$lock;
        } else {
            $url = "http://www.zhengfuz.com/index/Wx_User/wxInfo?lock=".$lock;
        }
        $redirect_uri = urlencode($url);
        $scope = "snsapi_userinfo";
        $state = "";
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope={$scope}&state={$state}#wechat_redirect";
        header('location:' . $url);
        //return $url;
    }


    //是否注册
//    public function isRegister(){
//        //$uid = $this->uid;
//        //if($uid && !empty($uid)){
//            $user_info = $this->user_model->with("userAttribute")->where("uid",$uid)->find();
//            if($user_info){
//                if($user_info["status"]==0){
//                    renderJson("该账户被封禁",0);
//                }
//                if($user_info["black"]==1){
//                    renderJson("该账户列入黑名单",0);
//                }
//                if($user_info["userAttribute"]->account_status==2){
//                    renderJson("该账户被禁用",0);
//                }
//                $this->userAttribute = $user_info["userAttribute"];
//                unset($user_info["userAttribute"]);
//                $this->userInfo = $user_info;
////            }else{
////                return $this->fetch("shop/register");
////            }
////        }else{
////            return $this->fetch("shop/register");
////        }
//
//
//    }
//

}